#include "party.h"
#include "map.h"

Party::Party()
{
    head = NULL;
    last = head;
    count = 0;
    x = -1;
    y = -1;
}

Party::Party(const Monster &add)
{
    addMember(add);
}

void Party::addMember(const Monster &in)
{
    if(head == NULL)
    {
        head = new List(new Monster(in));
        head->next = head;
        head->prev = head;
        last = head->next;

        count = 1;
    }
    else if (count == 1)
    {
        head->next = new List(new Monster(in), head, head);
        last = head->next;
        head->prev = last;
        count++;
    }
    else
    {
        last->next = new List(new Monster(in), last, head);
        last = last->next;
        head->prev = last;
        count++;
    }
    return;
}

string Party::getLeaderName()
{
    return head->character->getName();
}

//needs major rewrite
void Party::attackParty(Party &other)
{
    cout << getLeaderName() << "'s party challenges " << other.getLeaderName() << "'s party!\n";
    List* attackcurr = head;
    List* defendcurr = other.head;
    int attk, dfnd;
    while(!(isDefeated()) && !(other.isDefeated()))
    {
        cout << "\n\nLevel " << attackcurr->character->getLvl() << " " << attackcurr->character->getName() << "\n"
            << "vs. Level " << defendcurr->character->getLvl() << " " << defendcurr->character->getName() << "\n\n";

        attk = attackcurr->character->battleAction(1);
        cout << "Potential Damage: " << attk << "\n\n";
        dfnd = defendcurr->character->battleAction(2);
        cout << "Damage Blocked: " << dfnd << "\n\n";
        if(dfnd >= attk)
        {
            cout << "Attack Blocked!\n\n";
        }
        else
        {
            attk = attk - dfnd;
            defendcurr->character->setHealth(defendcurr->character->getHealth() - attk);
        }
        cout << "Counterattack!\n\n";
        attk = defendcurr->character->battleAction(1);
        cout << "Potential Damage: " << attk << "\n\n";
        dfnd = attackcurr->character->battleAction(2);
        cout << "Damage Blocked: " << dfnd << "\n\n";
        if(dfnd >= attk)
        {
            cout << "Attack Blocked!\n\n";
        }
        else
        {
            attk = attk - dfnd;
            attackcurr->character->setHealth(attackcurr->character->getHealth() - attk);
        }
        cout << "Next round of characters...\n\n";
        system("PAUSE");
        defendcurr = defendcurr->next;
        attackcurr = attackcurr->next;
    }
    if(other.isDefeated())
        cout << getLeaderName() << "'s party wins!\n\n";
    else
        cout << other.getLeaderName() << "'s party wins!\n\n";
}

bool Party::isDefeated()
{
    List* curr = head;
    if(!(curr->character->isDead()))
        return false;

    curr = curr->next;
    while(curr != head)
    {
        if(!(curr->character->isDead()))
            return false;
        curr = curr -> next;
    }
    return true;
}

void Party::idle()
{
    int choice = 0;
    cout << "What would you like to do?\n";
    cout << "1. LOOK\n";
    cout << "2. TALK\n";
    cout << "3. ATTACK\n";
    cout << "4. REST\n";
    cout << "5. GO NORTH\n";
    cout << "6. GO SOUTH\n";
    cout << "7. GO EAST\n";
    cout << "8. GO WEST\n";
    cout << "----------------------\n?: ";
    cin >> choice;
    cout << "\n\n";
    switch (choice)
    {
        case 1:
            look();
            break;
        case 2:
            talk();
            break;
        case 3:
            attackParty(*zone->GetLocation(x,y)->GetParty());
            break;
        case 4:
            if(zone->GetLocation(x,y)->GetParty() != (Party*)NULL)
                cout << "You can't!  There are monsters in the area!\n\n";
            else
            {
                cout << "All health returned!\n";
                rest();
            }
            break;
        case 5:
            if (zone->checkLocation(x, y - 1) == true)
            {
                setLocation(x,y-1);
                zone->GetLocation(x,y)->OnArrival(this);
            }
            else{ cout << "You can't go there.\n";}

            break;
        case 6:
            if(zone->checkLocation(x, y + 1) == true)
            {
                setLocation(x,y+1);
                zone->GetLocation(x,y)->OnArrival(this);
            }
            else{ cout << "You can't go there. \n";}
            break;
        case 7:
            if(zone->checkLocation(x + 1, y) == true)
            {
                setLocation(x + 1,y);
                zone->GetLocation(x,y)->OnArrival(this);
            }
            else{ cout << "You can't go there. \n";}
            break;
        case 8:
            if(zone->checkLocation(x - 1, y) == true)
            {
                setLocation(x - 1, y);
                zone->GetLocation(x,y)->OnArrival(this);
            }
            else{ cout << "You can't go there. \n";}
            break;
        default:
            cout << "Invalid choice... try again.\n";
    }

}

void Party::setLocation(int nx, int ny)
{
    x = nx;
    y = ny;
}

void Party::talk()
{
    zone->GetLocation(x,y)->GetNPC()->OnTalk();
}

void Party::look()
{
    zone->GetLocation(x,y)->OnArrival(this);
}


void Party::rest()
{
    List *tmp = head;
    tmp->character->HealthToMax();
    tmp = tmp->next;
    while (tmp != head)
    {
        tmp->character->HealthToMax();
        tmp = tmp->next;
    }
}

void Party::setMap(Map* mp)
{
    zone = mp;
}

/*void Party::critical()
{
    if (zone->GetLocation(x,y)->criticalOccurred() == FALSE)
    {*/
